package com.yiren.dbaa.module.practice_set.carmodel.model;

import android.app.Activity;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.yiren.backstage.currency.entity.CarModelEntity;
import com.yiren.dbaa.constant.AddressConstant;
import com.yiren.dbaa.currency.api.CommonCallBack;
import com.yiren.dbaa.module.practice_set.carmodel.contract.CarModelHomeContract;
import com.yiren.dbaa.net.OkHttpUtils;
import com.yiren.dbaa.net.bean.TestBaseBean;
import com.yiren.dbaa.net.interf.StringCallback;

import java.util.ArrayList;
import java.util.List;

/**
 * @author lwb
 * @date : 2022/12/5 14:56
 * @description: 车模首页
 */
public class CarModelHomeModel implements CarModelHomeContract.Model {

    private Activity activity;
    private Gson gson;

    public CarModelHomeModel(Activity activity) {
        this.activity = activity;
        gson = new Gson();
    }

    @Override
    public void getCarModelFromServer(String id, boolean isPublic, CommonCallBack callBack) {
        OkHttpUtils.getInstance().getStringByAsync(activity, AddressConstant.loadCarModelByUser("?userId=" + id), new StringCallback() {
            @Override
            public void getString(String result) {
                TestBaseBean<List<CarModelEntity>> testBaseBean = gson.fromJson(result, new TypeToken<TestBaseBean<List<CarModelEntity>>>() {
                }.getType());
                if (testBaseBean.isSuccess()) {
                    if (testBaseBean.getData() != null && !isPublic) {
                        List<CarModelEntity> carModelList = new ArrayList<>();
                        for (int i = 0; i < testBaseBean.getData().size(); i++) {
                            if (testBaseBean.getData().get(i).getState() == 1) {
                                carModelList.add(testBaseBean.getData().get(i));
                            }
                        }
                        callBack.success(carModelList);
                    } else {
                        callBack.success(testBaseBean.getData());
                    }
                } else {
                    callBack.fail("数据请求错误!");
                }
            }

            @Override
            public void getFail() {
                callBack.fail("数据请求失败!");
            }
        });

    }

    @Override
    public List<CarModelEntity> carModelHomeDataMerge(List<CarModelEntity> localModels, List<CarModelEntity> serverModels) {
        for (CarModelEntity localCar : localModels) {
            boolean exist = false;
            for (int i = 0; i < serverModels.size(); i++) {
                CarModelEntity serverCar = serverModels.get(i);
                serverCar.setCarModelVerLast(serverCar.getCarModelVer());
                //本地存在 以本地数据为准
                if (localCar.getId().equals(serverCar.getId())) {
                    //判断版本是否可更新
                    localCar.setCarModelVerLast(serverCar.getCarModelVer());
                    if (localCar.getCarModelVer() < serverCar.getCarModelVer()) {
                        localCar.setUpdateDetails(serverCar.getUpdateDetails());
                        localCar.setUpdate(true);
                    }
                    //判断本地文件是否存在
                    if (localCar.getCarModelVer() >= 0)
                        localCar.setDownLoad(true);
                    serverModels.set(i, localCar);
                    exist = true;
                    break;
                }
            }
            //新增
            if (!exist) {
                localCar.setDownLoad(true);
                serverModels.add(localCar);
            }
        }
        return serverModels;
    }

    @Override
    public void downLoadCarModelPoint(CarModelEntity carModel, CommonCallBack callBack) {
        OkHttpUtils.getInstance().getStringByAsync(activity, AddressConstant.downloadCarModel("?carModelId=" + carModel.getId() + "&carModelVer=" + carModel.getCarModelVer()), new StringCallback() {
            @Override
            public void getString(String result) {
                TestBaseBean<List<CarModelEntity>> testBaseBean = gson.fromJson(result, new TypeToken<TestBaseBean<List<CarModelEntity>>>() {
                }.getType());
                if (testBaseBean.isSuccess()) {
                    callBack.success(testBaseBean.getData());
                } else {
                    callBack.fail("下载失败!");
                }
            }

            @Override
            public void getFail() {
                callBack.fail("下载失败!");
            }
        });


    }
}
